﻿using CKS.Core.Framework;
using CKS.Core.Framework.Models;
using CKS.Schedule;
using Infrastructure;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZR.Model.Models.ProductionControl;

namespace CKS.PLC.Core.CronJob
{
    [Schedule]
    public class CacheDataJob : CronJobBase
    {
        IFreeSql<MesDB> fsql;
        public CacheDataJob() : base("查询数据", "0/1 * * * * ?") //每间隔1秒执行1次
        {
           fsql = InternalApp.Fsql;
        }



        private readonly object _lock = new object();
        private List<CksShelfForFixture> DataL = new List<CksShelfForFixture>();
        private List<CksShelfForFixture> DataR = new List<CksShelfForFixture>();

        public List<CksShelfForFixture> GetData(int m)
        {
            lock (_lock)
            {
                List<CksShelfForFixture> res = null;
                switch (m)
                {
                    case 1:
                        res= DataL.ToList();
                        break;
                    default:
                        res= DataR.ToList();
                        break;
                }
                if (res.Count == 0)
                {
                    res = new List<CksShelfForFixture>();
                    for (int i = 1; i <= 243; i++)
                    {
                        var item = new CksShelfForFixture();
                        item.Id = i;
                        item.CellId = i;
                        res.Add(item);
                    }
                }
                return res;
            }
        }

        protected override async Task ExecuteAsync(CancellationToken cancellationToken)
        {
            //区域
            var regions  = fsql.Select<DcOp80CurrentShelf>().ToList();
            lock (_lock)
            {
                //查询左料架信息
                DataL = fsql.Select<CksShelfForFixture>().Where(a => a.ShelfId == regions[0].ShelfId).OrderBy(a => a.CellId).ToList();
                //查询右料架信息
                DataR = fsql.Select<CksShelfForFixture>().Where(a => a.ShelfId == regions[1].ShelfId).OrderBy(a => a.CellId).ToList();
                //更新到当前缓存数据
            }
            var caches = fsql.Select<CksPartCache>().OrderBy(a => a.PartType).ToList();

            foreach (var part in caches)
            {
                var PartType = part.PartType;
                int CleaningLineCount = fsql.Select<CksBasket>().Where(a => a.Type == PartType && a.Status == 1).Sum(a => a.Stock).ToInt();
                int CacheLineCount = fsql.Select<CksBasket>().Where(a => a.Type == PartType && a.Status == 2).Sum(a => a.Stock).ToInt();
                fsql.Update<CksPartCache>()
                    .Set(a => a.CleaningLineCount, CleaningLineCount)
                    .Set(a => a.CacheLineCount, CacheLineCount)
                    .Where(a => a.PartType == PartType)
                    .ExecuteAffrows();
            }

            



            await Task.CompletedTask;
        }
    }
}
